package breeze.pixel.weather.apps_util.utils.appsettings.settingsview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSeekBar;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.slider.Slider;

import breeze.pixel.weather.App;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.MDialog;
import breeze.pixel.weather.apps_util.utils.MToast;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;
import breeze.pixel.weather.apps_util.views.AppLinearLayout;

public class SettingsCardView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_card_view);
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        init();
        initData();
    }

    private void initData() {
        SettingsCardViewTransSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                appLinearLayout.setRadiusAndTrans(appLinearLayout.getRadius(),progress);
                appLinearLayout.setTransparency(progress);
                setTransText(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        SettingsCardViewRadiusSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                appLinearLayout.setRadiusAndTrans(progress,appLinearLayout.getTransparency());
                appLinearLayout.setRadius(progress);
                setRadiusText(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private AppLinearLayout appLinearLayout;
    private AppCompatSeekBar SettingsCardViewTransSlider;
    private AppCompatSeekBar SettingsCardViewRadiusSlider;
    private TextView SettingsCardViewAppTransTv;
    private TextView SettingsCardViewAppRadiusTv;
    private void init() {
        appLinearLayout = findViewById(R.id.SettingsCardViewAppLinearLayout);
        SettingsCardViewTransSlider = findViewById(R.id.SettingsCardViewTransSlider);
        SettingsCardViewTransSlider.setProgress(appLinearLayout.getTransparency());
        SettingsCardViewRadiusSlider = findViewById(R.id.SettingsCardViewRadiusSlider);
        SettingsCardViewRadiusSlider.setProgress(appLinearLayout.getRadius());
        SettingsCardViewAppTransTv = findViewById(R.id.SettingsCardViewAppTransTv);
        SettingsCardViewAppRadiusTv = findViewById(R.id.SettingsCardViewAppRadiusTv);
        setTransText(appLinearLayout.getTransparency());
        setRadiusText(appLinearLayout.getRadius());
    }

    @SuppressLint("SetTextI18n")
    private void setTransText(int value){
        SettingsCardViewAppTransTv.setText("透明度:"+value);
    }

    @SuppressLint("SetTextI18n")
    private void setRadiusText(int value){
        SettingsCardViewAppRadiusTv.setText("圆角度:"+value);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    public void save(View view) {
        MToast.show(this,"保存成功");
        AppSettings appSettings = App.getAppSettings();
        appSettings.setCardViewTransparency(appLinearLayout.getTransparency());
        appSettings.setCardViewRadius(appLinearLayout.getRadius());
        Intent intent = new Intent("breeze.pixel.weather.action.target.AppLinearLayout");
        sendBroadcast(intent);
    }
}